var services = services || {};

(function($) {
	/**
	 * 
	 */
	services.customer = {
			
			config : {
				emailIdCgprefix : "emailCG-",
				emailIdTrashprefix : "trash-",
				phoneIdCgprefix : "phoneCG-",
				phoneIdTrashprefix : "trash-"
			},
			
			removeEmail : function(trashId){
				//get index
				var idTrashprefix = this.config.emailIdTrashprefix;
				var index = trashId.substring(trashId.indexOf(idTrashprefix)+idTrashprefix.length);
				
				//get ControlGourp with id
				var idCgprefix = this.config.emailIdCgprefix;
				var idCg = idCgprefix+index;
				var div = jQuery('div[id="'+idCg+'"]:last');

				//move the add button to last row
				var lastDivId = $('div[id^="'+idCgprefix+'"]:last').attr("id");
				if(lastDivId == idCg){
					//if remove last CG
					//move add button the CG before the last
					var newIndex = parseInt(index)-1
					lastDivId = idCgprefix + newIndex;
				}
				//calc number of CG
				var nbCG = $("#emailInputGroup > .control-group").length
				if(nbCG==1){
					//move button to fieldset
					$("#emailInputGroup").append($("#emailInputGroup .icon-plus"));
				}else{
					//move add button to CG
					$("#"+lastDivId+" .controls").append($("#emailInputGroup .icon-plus"));
				}
				//destroy div
				div.remove();
				
				//check limit
				var hasReachNbMaxEmail = services.customer.hasReachNbMaxEmail();
				if(!hasReachNbMaxEmail){
					//show warning
					$("#nbMaxEmailWarning").hide();
					
					//hide add button
					$("#emailInputGroup .icon-plus").show();
				}
			},
			addEmail : function(){
				//get email template
				var emailTemplate = $("#emailExample").html();
				
				//get index (get max index)
				var idCgprefix = this.config.emailIdCgprefix;
				var lastDiv = $('#emailInputGroup div[id^="'+idCgprefix+'"]:last');
				var lastDivId = lastDiv.attr("id");
				
				var newIndex = 0;
				if(lastDivId != null && lastDivId != "undified"){
					var index = lastDivId.substring(lastDivId.indexOf(idCgprefix)+idCgprefix.length);
					newIndex = parseInt(index)+1;
				}
				
				//replace index in template
				var emailTemplateToInsert = emailTemplate.replace(new RegExp("(IndexToReplace)", 'g'),newIndex);;
				var idCg = idCgprefix+newIndex;
				
				//insert template just after last email
				//use class 'emailInput' in element 'emailInputGroup'
				$("#emailInputGroup").append(emailTemplateToInsert);
				
				//move the add button to last row
				$("#"+idCg+" .controls").append($("#emailInputGroup .icon-plus"));
				
				//activate input
				$("#"+idCg+ " input").removeAttr("disabled");
				
				//focus on input
				$("#"+idCg+ " input[name$='.email']").focus();
				
				//check limit
				var hasReachNbMaxEmail = services.customer.hasReachNbMaxEmail();
				if(hasReachNbMaxEmail){
					//show warning
					$("#nbMaxEmailWarning").show();
					
					//hide add button
					$("#emailInputGroup .icon-plus").hide();
				}
			},
			hasReachNbMaxEmail:function(){
				//get limit to reach
				var nbMaxEmail = $("#nbMaxEmail").val();
				
				//calculate nb of email displayed
				//ie count nb of control-group in emailInputGroup
				var nbCG = $("#emailInputGroup > .control-group").length;
				
				return nbCG >= nbMaxEmail;
			},
			
			removePhone : function(trashId){
				//get index
				var idTrashprefix = this.config.phoneIdTrashprefix;
				var index = trashId.substring(trashId.indexOf(idTrashprefix)+idTrashprefix.length);
				
				//get ControlGourp with id
				var idCgprefix = this.config.phoneIdCgprefix;
				var idCg = idCgprefix+index;
				var div = jQuery('div[id="'+idCg+'"]:last');

				//move the add button to last row
				var lastDivId = $('div[id^="'+idCgprefix+'"]:last').attr("id");
				if(lastDivId == idCg){
					//if remove last CG
					//move add button the CG before the last
					var newIndex = parseInt(index)-1
					lastDivId = idCgprefix + newIndex;
				}
				//calc number of CG
				var nbCG = $("#phoneInputGroup > .control-group").length
				if(nbCG==1){
					//move button to fieldset
					$("#phoneInputGroup").append($("#phoneInputGroup .icon-plus"));
				}else{
					//move add button to CG
					$("#"+lastDivId+" .controls").append($("#phoneInputGroup .icon-plus"));
				}
				//destroy div
				div.remove();
				
				//check limit
				var hasReachNbMaxPhone = services.customer.hasReachNbMaxPhone();
				if(!hasReachNbMaxPhone){
					//show warning
					$("#nbMaxPhoneWarning").hide();
					
					//hide add button
					$("#phoneInputGroup .icon-plus").show();
				}
			},
			addPhone : function(){
				//get phone template
				var phoneTemplate = $("#phoneExample").html();
				
				//get index (get max index)
				var idCgprefix = this.config.phoneIdCgprefix;
				var lastDiv = $('#phoneInputGroup div[id^="'+idCgprefix+'"]:last');
				var lastDivId = lastDiv.attr("id");
				
				var newIndex = 0;
				if(lastDivId != null && lastDivId != "undified"){
					var index = lastDivId.substring(lastDivId.indexOf(idCgprefix)+idCgprefix.length);
					newIndex = parseInt(index)+1;
				}
				
				//replace index in template
				var phoneTemplateToInsert = phoneTemplate.replace(new RegExp("(IndexToReplace)", 'g'),newIndex);;
				var idCg = idCgprefix+newIndex;
				
				//insert template just after last phone
				//use class 'phoneInput' in element 'phoneInputGroup'
				$("#phoneInputGroup").append(phoneTemplateToInsert);
				
				//move the add button to last row
				$("#"+idCg+" .controls").append($("#phoneInputGroup .icon-plus"));
				
				//activate input
				$("#"+idCg+ " input").removeAttr("disabled");
				
				//focus on input
				$("#"+idCg+ " input[name$='.phone']").focus();
				
				//check limit
				var hasReachNbMaxPhone = services.customer.hasReachNbMaxPhone();
				if(hasReachNbMaxPhone){
					//show warning
					$("#nbMaxPhoneWarning").show();
					
					//hide add button
					$("#phoneInputGroup .icon-plus").hide();
				}
			},
			hasReachNbMaxPhone:function(){
				//get limit to reach
				var nbMaxPhone = $("#nbMaxPhone").val();
				
				//calculate nb of phone displayed
				//ie count nb of control-group in phoneInputGroup
				var nbCG = $("#phoneInputGroup > .control-group").length;
				
				return nbCG >= nbMaxPhone;
			}
	};
})(jQuery);
